package com.cg.controller;

import com.cg.dto.Result;
import com.cg.dto.ResultUtils;
import com.cg.entity.Meetingroom;
import com.cg.service.MeetingroomService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * (Meetingroom)表控制层
 *
 * @author chengong
 * @since 2023-12-18 09:02:49
 */
@RestController
@RequestMapping("meeting")
public class MeetingroomController {
    /**
     * 服务对象
     */
    @Resource
    private MeetingroomService meetingroomService;

    /**
     * 查询全部会议室预定情况
     */
    @GetMapping("/queryMeetingRoomAll")
    public Result<List<Meetingroom>> queryMeetingRoomAll(){
        List<Meetingroom> meetingrooms = meetingroomService.queryMeetingRoomAll();
        return ResultUtils.returnDataSuccess(meetingrooms);
    }


    /**
     * 查询所有的会议室名字
     */
    @GetMapping("/queryMeetingRoomName")
    public Result<List<Meetingroom>> queryMeetingRoomName(){
        List<Meetingroom> meetingrooms = meetingroomService.queryMeetingRoomName();
        return ResultUtils.returnDataSuccess(meetingrooms);
    }

    /**
     * 会议室预定
     */
    @PostMapping("/insertMeeting")
    public Result insertMeeting(@RequestBody Meetingroom meetingroom){
        String meetingOrder = meetingroom.getMeetingOrder();
        String regex = "^\\d{4}-\\d{2}-\\d{2}$";

        Pattern pattern = Pattern.compile(regex);

        Matcher matcher = pattern.matcher(meetingOrder);

        if (!matcher.matches()) {
            return ResultUtils.returnFail("时间格式不正确");
        }
        Integer count = meetingroomService.insertMeeting(meetingroom);
        if(count <= 0){
            return ResultUtils.returnFail("输入有误");
        }
        return ResultUtils.returnSuccess("预定会议室成功");
    }

}

